//
//  ViewControllerForNews.m
//  世界
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ViewControllerForNews.h"
#import "CellForFocusNews.h"
#import "CellForChineseMilitary.h"
#import "CellForInternationalMilitary.h"
#import "CellForHistory.h"

@interface ViewControllerForNews ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UISegmentedControl *seg;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UICollectionView *collection;

@end

@implementation ViewControllerForNews

#pragma mark 重新dealloc方法
- (void)dealloc
{
    [super dealloc];
    
    [_flowLayout release];
    
    [_collection release];
    
    [_seg release];
}




#pragma mark 视图的生命周期 >>>>>>
#pragma mark 视图加载完成
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    self.navigationItem.title = @"世界";
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    [self addSegmentedControl];
    
    [self createCollectionView];
    
    [self createKVO];
}

#pragma mark 视图即将显示
- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh" object:nil];
}



#pragma mark 接受到内存警告!!!
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



#pragma mark 自定义方法 >>>>>>
#pragma mark 创建UICollectionViewFlowLayout
- (void)createFlowLayout
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //简单布局API:
    self.flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height-64-49 - 30);
    
    //item之间的最小间距(纵向)
    self.flowLayout.minimumLineSpacing = 0;
    
    //滑动方向 (水平)
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}





#pragma mark 创建UICollectionView
- (void)createCollectionView
{
    [self createFlowLayout];
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 94, self.view.frame.size.width, self.view.frame.size.height-64 - 49 - 30) collectionViewLayout:self.flowLayout];
    
    [_flowLayout release];
    
    self.collection.backgroundColor = [UIColor whiteColor];
    
    //不显示水平方向的滑动条
    self.collection.showsHorizontalScrollIndicator = NO;
    
    //开启翻页效果
    self.collection.pagingEnabled = YES;
    
    [self.view addSubview: self.collection];
    
    [_collection release];
    
    
    //指定代理人
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    //注册视频列表的Cell类型 (Cell 只支持先注册)
    [self.collection registerClass:[CellForFocusNews class] forCellWithReuseIdentifier:@"pool1"];
    
    [self.collection registerClass:[CellForChineseMilitary class] forCellWithReuseIdentifier:@"pool2"];
    
    [self.collection registerClass:[CellForInternationalMilitary class] forCellWithReuseIdentifier:@"pool3"];
    
    [self.collection registerClass:[CellForHistory class] forCellWithReuseIdentifier:@"pool4"];
}




#pragma mark UICollectionViewDataSource的协议方法 >>>>>>
#pragma mark 返回Item的个数 (required 1/2)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}




#pragma mark 给每个Item山添加Cell (required 2/2)
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    /**
     * 第一个Item上添加视频类的Cell
     * 第二个Item上添加电台类的Cell
     */
    
    switch (indexPath.item)
    {
        case 0:
        {
            CellForFocusNews *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
            
            cell.backgroundColor = [UIColor redColor];
            
            return cell;
        }
            break;
            
        case 1:
        {
            CellForChineseMilitary *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool2" forIndexPath:indexPath];
            
            cell.backgroundColor = [UIColor blueColor];
            
            return cell;
        }
            break;
            
            
        case 2:
        {
            CellForInternationalMilitary *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool3" forIndexPath:indexPath];
            
            cell.backgroundColor = [UIColor greenColor];
            
            return cell;
        }
            break;
            
        case 3:
        {
            CellForHistory *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool4" forIndexPath:indexPath];
            
            cell.backgroundColor = [UIColor orangeColor];
            
            return cell;
        }
            break;
    }
    return nil;
}




#pragma mark 添加分段控制
- (void)addSegmentedControl
{
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"要闻", @"中国军情", @"国际军情", @"历史"]];
    
    self.seg.frame = CGRectMake(0, 64, self.view.frame.size.width, 30);
    
    [self.view addSubview:self.seg];
    
    //self.navigationItem.titleView = self.seg;
    
    // 更改SegmentedControl字体相关API
    self.seg.tintColor = [UIColor clearColor];
    
    // 设置放大 颜色
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor redColor]};
    
    [self.seg setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor grayColor]};
    
    [self.seg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    [self.seg release];
    
    // 要闻为默认
    self.seg.selectedSegmentIndex = 1;
    
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
}




#pragma mark 点击分段控制所触发的方法
- (void)segAction:(UISegmentedControl *)seg
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:seg.selectedSegmentIndex inSection:0];
    
    [self.collection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}


#pragma mark 添加键值观察者
- (void)createKVO
{
    //观察collection的contentOffset的属性
    [self.collection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"context"];
}




#pragma mark 当被观察值属性发生变换的时候调用此方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    
    
    
    
    //collection的contentOffset的属性发生变化后的X的值
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    
    //为0时
    if (x == self.view.frame.origin.x)
    {
        //分段控制器选择视频项
        self.seg.selectedSegmentIndex = 0;
    }
    
    //为屏幕的宽时
    if (x == self.view.frame.size.width)
    {
        //分段控制选择电台项
        self.seg.selectedSegmentIndex = 1;
    }
    
    if (x == self.view.frame.size.width *2)
    {
        //分段控制选择电台项
        self.seg.selectedSegmentIndex = 2;
    }
    
    if (x == self.view.frame.size.width *3)
    {
        //分段控制选择电台项
        self.seg.selectedSegmentIndex = 3;
    }
}






@end
